package com.gzyj.jtt808.v2011.protocol.jtt1078;

import com.gzyj.jtt808.utils.HexBytesUtil;
import io.netty.buffer.ByteBuf;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author lhj
 */
@ApiModel("终端上传音视频资源列表")
@Data
public class JTT808_0x1205_AVResouces implements Serializable {

    @ApiModelProperty("逻辑通道号")
    private Byte LogicChannelNo;

    @ApiModelProperty("开始时间")
    private Date BeginTime;

    @ApiModelProperty("结束时间")
    private Date EndTime;

    @ApiModelProperty("报警标志")
    private Long AlarmFlag;

    @ApiModelProperty("音视频资源类型")
    private Byte AVResourceType;

    @ApiModelProperty("码流类型")
    private Byte StreamType;

    @ApiModelProperty("存储器类型")
    private Byte MemoryType;

    @ApiModelProperty("文件大小")
    private Long FileSize;

    public void encode(ByteBuf byteBuf){
        SimpleDateFormat sdf=new SimpleDateFormat("yyMMddHHmmss");
        byteBuf.writeByte(this.LogicChannelNo);
        byteBuf.writeBytes(HexBytesUtil.WriteBCD(sdf.format(this.BeginTime),12));
        byteBuf.writeBytes(HexBytesUtil.WriteBCD(sdf.format(this.EndTime),12));
        byteBuf.writeLong(this.AlarmFlag);
        byteBuf.writeByte(this.AVResourceType);
        byteBuf.writeByte(this.StreamType);
        byteBuf.writeByte(this.MemoryType);
        byteBuf.writeInt(this.FileSize.intValue());

    }

    public JTT808_0x1205_AVResouces decode(ByteBuf byteBuf){
        SimpleDateFormat sdf=new SimpleDateFormat("yyMMddHHmmss");
        this.LogicChannelNo=byteBuf.readByte();
        try {
            this.BeginTime=sdf.parse(HexBytesUtil.ReadBCD(HexBytesUtil.readByOffset(byteBuf,6),12));
            this.EndTime=sdf.parse(HexBytesUtil.ReadBCD(HexBytesUtil.readByOffset(byteBuf,6),12));
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        this.AlarmFlag=byteBuf.readLong();
        this.AVResourceType=byteBuf.readByte();
        this.StreamType= byteBuf.readByte();
        this.MemoryType=byteBuf.readByte();
        this.FileSize=byteBuf.readUnsignedInt();
        return this;
    }

}
